﻿

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>用户管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">登录账户</label>
            <div class="layui-input-block">
              <input type="text" name="txt_loginid" id="txt_loginid" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
              <input type="text" name="txt_username" id="txt_username" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="sel_roles" id="sel_roles">
                  
                </select>
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">是否可用</label>
            <div class="layui-input-block">
              <select name="sel_isactive" id="sel_isactive">
                <option value="">全部</option>
                <option value="Y" selected>可用</option>
                <option value="N">不可用</option>
              </select>
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      
      <div class="layui-card-body">
          <div style="padding-bottom: 10px;">
              <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">禁用</button>
              <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
          </div>
          <script type="text/html" id="buttonTpl">
              {{#  if(d.isactive == "Y"){ }}
              <button class="layui-btn layui-btn-xs layui-btn-xs">正常</button>
              {{#  } else if(d.isactive == "N"){ }}
              <button class="layui-btn layui-btn-danger layui-btn-xs">禁用</button>
              {{#  } else { }}
              <button class="layui-btn layui-btn-primary layui-btn-xs">其他</button>
              {{#  } }}
          </script>
          <script type="text/html" id="test-table-operate-td">
              {{#  if(d.isactive == "Y"){ }}
              <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">禁用</a>
              <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
              {{#  } else if(d.isactive == "N"){ }}
              <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
              <a class="layui-btn layui-btn-xs layui-btn-xs" lay-event="start">启用</a>
              <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
              {{#  } else { }}
              <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
              {{#  } }}
          </script>
          <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
          <script type="text/html" id="imgTpl">
              <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
          </script>
          <script type="text/html" id="table-useradmin-webuser">
              <a class="layui-btn layui-btn-normal layui-btn-xs"  lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>禁用</a>
          </script>
      </div>
    </div>
  </div>
  <script src="../../../layuiadmin/layui/layui.js"></script>  
  <script src="../../../layuiadmin/js/cookie.js"></script>
  <script>
  layui.config({
    base: '../../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'useradmin', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
          , table = layui.table;

      //验证用户是否登录
      if ($.cookie("loginid") == "" || $.cookie("loginid") == null || $.cookie("loginid") == "null" || $.cookie("loginid") == undefined || $.cookie("loginid") == "undefined") {
          $.removeCookie("id", { path: "/", sucue: true });//用户id
          $.removeCookie("loginid", { path: "/", sucue: true });//登录账号
          $.removeCookie("username", { path: "/", sucue: true });//用户名
          $.removeCookie("role", { path: "/", sucue: true });//权限
          window.location.href = "user/login.html";
      }
      //加载表格
      table.render({
          elem: '#LAY-user-manage'
          , id: 'testReload'//容器的ID
          , url: 'ashx/list.ashx?type=getlist'
          , toolbar: '#test-table-radio-toolbarDemo'
          , method: 'post'
          , page: true
          , limits: [10, 20, 30]
          , limit: 10
          , cols: [[
              { type: 'checkbox' }
              , { field: 'id', title: 'id', hide: true }
              , { field: 'loginid', width: 120, title: '登录账户', align: 'center' }
              , { field: 'username', width: 120, title: '用户名', align: 'center' }
              , { field: 'customername', width: 120, title: '联系人', align: 'center' }
              //, { field: 'email', width: 180, title: '邮箱', align: 'center' }
              , { field: 'phone', width: 120, title: '手机号', align: 'center' }
              //, { field: 'sex', width: 60, title: '性别', align: 'center' }
              //, { field: 'manage_storename', width: 200, title: '管辖店铺', align: 'center' }
              //, { field: 'imgurl', width: 100, title: '头像', align: 'center', templet: img }
              , { field: 'glqy', width: 120, title: '所属区域', align: 'center' }
              , { field: 'glyw', width: 120, title: '所属业务', align: 'center' }
              , { field: 'glsp', width: 120, title: '所属商品', align: 'center' }
              , { field: 'isactive', width: 80, title: '状态', align: 'center', templet: '#buttonTpl' }
              , { field: 'createtime', width: 180, title: '创建时间', align: 'center', sort: true }
              , { field: 'modifytime', width: 180, title: '修改时间', align: 'center', sort: true }
              , { width: 178,title:"操作", align: 'center', fixed: 'right', toolbar: '#test-table-operate-td' }

          ]], parseData: function (res) { //将原始数据解析成 table 组件所规定的数据.
              return {
                  "code": res.code, //解析接口状态
                  "msg": res.msg, //解析提示文本
                  "count": res.count, //解析数据长度
                  "data": res.data //解析数据列表
              };
          }
          , where: {
              txt_loginid: $('#txt_loginid').val(),
              txt_username: $('#txt_username').val(),
              sel_roles: $('#sel_roles').val(),
              sel_isactive: $('#sel_isactive').val()
          }
      });

      //加载权限选项下拉框
      $.ajax({
          type: "post",
          url: "ashx/list.ashx?type=get_roles",
          dataType: "json",
          async: false,
          success: function (data) {
              var html = '';
              if (data.data.length == 0) {
                  html += "<option value=''>无数据</option>";
                  $("#sel_roles").empty().append(html);
              } else {
                  html += "<option value=''>请选择数据</option>";
                  for (var i = 0; i < data.data.length; i++) {
                      html += "<option value='" + data.data[i].id + "'>" + data.data[i].rolesname + "</option>"
                  }
                  $("#sel_roles").empty().append(html);
              }
              form.render();
          },
          error: function (data) {
              layer.error(data);
          }
      });
      // 表格格式化  -  img 渲染
      function img(d) {
          return "<img src='" + d.imgurl + "' height=\"30\" width=\"30\" alt='icon'/>";
      }
      //监听排序
      table.on('sort(LAY-user-manage)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
          table.reload('testReload', {//刷新列表
              initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。 layui 2.1.1 新增参数
              , where: { //请求参数
                  field: obj.field //排序字段
                  , order: obj.type //排序方式
                  , txt_loginid: $('#txt_loginid').val()
                  , txt_username: $('#txt_username').val()
                   ,sel_roles: $('#sel_roles').val()
                   ,sel_isactive: $('#sel_isactive').val()
              }
          });
      });
      //监听工具条
      table.on('tool(LAY-user-manage)', function (obj) {
          var data = obj.data;
          var editid = data.id;
          if (obj.event === 'detail') {
              layer.msg('ID：' + data.id + ' 的查看操作');
          }
          else if (obj.event === 'del') {
              layer.confirm('确定要禁用吗', function (index) {
                  $.ajax({
                      url: 'ashx/list.ashx?type=delete&getids=' + data.id,
                      type: "post",
                      contentType: "application/json",
                      dataType: "json",
                      data: {},
                      success: function (res) {
                          var index = parent.layer.getFrameIndex(window.name);
                          if (res.flag == "True") {
                              layer.msg(res.remsg, {
                                  time: 2000,
                                  end: function () {
                                      //执行重载
                                      table.reload('testReload', {
                                          where: {
                                              txt_loginid: $('#txt_loginid').val(),
                                              txt_username: $('#txt_username').val(),
                                              sel_roles: $('#sel_roles').val(),
                                              sel_isactive: $('#sel_isactive').val()
                                          }, page: {
                                              curr: 1 //重新从第 1 页开始
                                          }
                                      });
                                  }
                              })
                          }
                          else {
                              layer.alert(res.remsg);
                          }
                      }
                  });
              });
          }
          else if (obj.event === 'start') {
              layer.confirm('确定要启用吗', function (index) {
                  $.ajax({
                      url: 'ashx/list.ashx?type=start&getids=' + data.id,
                      type: "post",
                      contentType: "application/json",
                      dataType: "json",
                      data: {},
                      success: function (res) {
                          var index = parent.layer.getFrameIndex(window.name);
                          if (res.flag == "True") {
                              layer.msg(res.remsg, {
                                  time: 2000,
                                  end: function () {
                                      //执行重载
                                      table.reload('testReload', {
                                          where: {
                                              txt_loginid: $('#txt_loginid').val(),
                                              txt_username: $('#txt_username').val(),
                                              sel_roles: $('#sel_roles').val(),
                                              sel_isactive: $('#sel_isactive').val()
                                          }, page: {
                                              curr: 1 //重新从第 1 页开始
                                          }
                                      });
                                  }
                              })
                          }
                          else {
                              layer.alert(res.remsg);
                          }
                      }
                  });
              });
          }
          else if (obj.event === 'edit') {
             var index= layer.open({
                  type: 2
                  , title: '修改用户'
                  , content: 'edit.html?editid=' + data.id
                  , maxmin: true
                  , area: ['700px', '750px']
                  , btn: ['确定', '取消']
                  , yes: function (index, layero) {
                      var iframeWindow = window['layui-layer-iframe' + index]
                          , submitID = 'LAY-user-front-submit'
                          , submit = layero.find('iframe').contents().find('#' + submitID);
                      //监听提交
                      iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                          var obj = $(layero).find("iframe")[0].contentWindow;
                          var txt_loginid = obj.$("#txt_loginid").val();//登录账户
                          var txt_username = obj.$("#txt_username").val();//登录名
                          var customername = obj.$("#customername").val();//经销商联系人
                          var password = obj.$("#password").val();//密码
                          var phone = obj.$("#phone").val();//手机号
                          var email = obj.$("#email").val();//邮箱
                          //var imgurl = obj.$("#imgurl").val();//头像图片链接
                          var imgurl = "";//头像图片链接
                          //var managestore = obj.$("#managestore").val();//管辖店铺名称
                          //var storeid = obj.$("#storeid").val();//管辖店铺id

                          var managestore ="";//管辖店铺名称
                          var storeid = "";//管辖店铺id
                          var sel_roles = obj.$("select[name='sel_roles']").val();//角色
                          var sel_manageqy = obj.$("select[name='sel_manageqy']").val();//管理区域
                          var sel_manageyw = obj.$("select[name='sel_manageyw']").val();//管理业务
                          var sel_managesp = obj.$("select[name='sel_managesp']").val();//管理商品
                          var sex = obj.$('input[name="sex"]:checked').val();//性别
                          var isactive = obj.$("#isactive").prop("checked");//是否可用
                          var islookme = obj.$("#islookme").prop("checked");//是否查看自己
                          var arr = new Array();
                          if (txt_loginid == "" || txt_loginid == null || txt_loginid == undefined) {
                              layer.msg('请填写登录账户', { icon: 5 });
                              return;
                          }
                          if (txt_username == "" || txt_username == null || txt_username == undefined) {
                              layer.msg('请填写登录名', { icon: 5 });
                              return;
                          }
                          if (customername == "" || customername == null || customername == undefined) {
                              layer.msg('请填写经销商联系人', { icon: 5 });
                              return;
                          }
                          if (password == "" || password == null || password == undefined) {
                              layer.msg('请填写密码', { icon: 5 });
                              return;
                          }
                          if (phone == "" || phone == null || phone == undefined) {
                              layer.msg('请填写手机号', { icon: 5 });
                              return;
                          }
                          //if (email == "" || email == null || email == undefined) {
                          //    layer.msg('请填写邮箱', { icon: 5 });
                          //    return;
                          //}
                          //if (storeid == "" || storeid == null || storeid == undefined) {
                          //    layer.msg('请选择管辖店铺', { icon: 5 });
                          //    return;
                          //}
                          if (sel_roles == "" || sel_roles == null || sel_roles == undefined) {
                              layer.msg('请选择角色', { icon: 5 });
                              return;
                          }
                          if (sel_manageqy == "" || sel_manageqy == null || sel_manageqy == undefined) {
                              layer.msg('请选择管理区域', { icon: 5 });
                              return;
                          }
                          if (sel_manageyw == "" || sel_manageyw == null || sel_manageyw == undefined) {
                              layer.msg('请选择管理业务', { icon: 5 });
                              return;
                          }
                          if (sel_managesp == "" || sel_managesp == null || sel_managesp == undefined) {
                              layer.msg('请选择管理商品', { icon: 5 });
                              return;
                          }
                          if (txt_loginid != "") {
                              var add = {
                                  "id": editid,
                                  "txt_loginid": txt_loginid,
                                  "txt_username": txt_username,
                                  "password": password,
                                  "phone": phone,
                                  "email": email,
                                  "imgurl": imgurl,
                                  "managestore": managestore,
                                  "storeid": storeid,
                                  "customername": customername,
                                  "sel_manageqy": sel_manageqy,
                                  "sel_manageyw": sel_manageyw,
                                  "sel_managesp": sel_managesp,
                                  "sel_roles": sel_roles,
                                  "sex": sex,
                                  "isactive": isactive,
                                  "islookme": islookme
                              };
                              arr.push(add);
                              $.ajax({
                                  url: 'ashx/list.ashx?type=edit',
                                  type: "post",
                                  contentType: "application/json",
                                  dataType: "json",
                                  data: JSON.stringify({ 'LoadData_type': arr }),
                                  success: function (res) {
                                      if (res.flag == "True") {
                                          layer.close(index);
                                          layer.msg(res.remsg, {
                                              time: 2000,
                                              end: function () {
                                                  //执行重载
                                                  table.reload('testReload', {
                                                      where: {
                                                          txt_loginid: $('#txt_loginid').val(),
                                                          txt_username: $('#txt_username').val(),
                                                          sel_roles: $('#sel_roles').val(),
                                                          sel_isactive: $('#sel_isactive').val()
                                                      }, page: {
                                                          curr: 1 //重新从第 1 页开始
                                                      }
                                                  });
                                              }
                                          })
                                      }
                                      else {
                                          layer.alert(res.remsg);
                                      }
                                  }
                              })
                          }
                      });
                      submit.trigger('click');
                  }
              }); 
              layer.full(index);
          }
      });
    //监听搜索
    form.on('submit(LAY-user-front-search)', function(data){
      var field = data.field;
        //执行重载
        table.reload('testReload', {
            where: {
                txt_loginid: $('#txt_loginid').val(),
                txt_username: $('#txt_username').val(),
                sel_roles: $('#sel_roles').val(),
                sel_isactive: $('#sel_isactive').val()
            }, page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    });
      function getRowFexId(value) {
          var checkStatus = table.checkStatus('testReload')
              , data = checkStatus.data, arr = new Array();;
          for (var i = 0; i < data.length; i++) {
              arr.push(data[i][value]);
          }
          return arr.join(",");
      }
    //事件
    var active = {
        batchdel: function () {
            var checkStatus = table.checkStatus('testReload')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            var getid = getRowFexId("id");
            layer.confirm('确定禁用吗？', function (index) {
                //执行 Ajax 后重载
                $.ajax({
                    url: 'ashx/list.ashx?type=delete&getids=' + getid,
                    type: "post",
                    contentType: "application/json",
                    dataType: "json",
                    data: {},
                    success: function (res) {
                        var index = parent.layer.getFrameIndex(window.name);
                        if (res.flag == "True") {
                            layer.msg(res.remsg, {
                                time: 2000,
                                end: function () {
                                    //执行重载
                                    table.reload('testReload', {
                                        where: {
                                            txt_loginid: $('#txt_loginid').val(),
                                            txt_username: $('#txt_username').val(),
                                            sel_roles: $('#sel_roles').val(),
                                            sel_isactive: $('#sel_isactive').val()
                                        }, page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    });
                                }
                            })
                        }
                        else {
                            layer.alert(res.remsg);
                        }
                    }
                });
            });
        }
      ,add: function(){
       var index= layer.open({
          type: 2
          ,title: '添加用户'
          ,content: 'add.html'
          ,maxmin: true
          ,area: ['700px', '750px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
            ,submitID = 'LAY-user-front-submit'
            ,submit = layero.find('iframe').contents().find('#'+ submitID);
            //监听提交
              iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                var obj = $(layero).find("iframe")[0].contentWindow;
                  var txt_loginid = obj.$("#txt_loginid").val();//登录账户
                  var txt_username = obj.$("#txt_username").val();//登录名
                var password = obj.$("#password").val();//密码
                var phone = obj.$("#phone").val();//手机号
                  var email = obj.$("#email").val();//邮箱
                  var customername = obj.$("#customername").val();//经销商联系人
                //var imgurl = obj.$("#imgurl").val();//头像图片链接
                var imgurl = "";//头像图片链接
                //var managestore = obj.$("#managestore").val();//管辖店铺名称
                //var storeid = obj.$("#storeid").val();//管辖店铺id
                  var managestore = "";//管辖店铺名称
                  var storeid ="";//管辖店铺id
                  var sel_roles = obj.$("select[name='sel_roles']").val();//角色
                  var sel_manageqy = obj.$("select[name='sel_manageqy']").val();//管理区域
                  var sel_manageyw = obj.$("select[name='sel_manageyw']").val();//管理业务
                  var sel_managesp = obj.$("select[name='sel_managesp']").val();//管理商品
                var sex = obj.$('input[name="sex"]:checked').val();//性别
                var isactive = obj.$("#isactive").prop("checked");//是否可用
                var islookme = obj.$("#islookme").prop("checked");//是否查看自己
                  var arr = new Array();
                  if (txt_loginid == "" || txt_loginid == null || txt_loginid == undefined) {
                    layer.msg('请填写登录账户', { icon: 5 });
                    return;
                }
                  if (txt_username == "" || txt_username == null || txt_username == undefined) {
                    layer.msg('请填写登录名', { icon: 5 });
                    return;
                  }
                  if (customername == "" || customername == null || customername == undefined) {
                      layer.msg('请填写经销商联系人', { icon: 5 });
                      return;
                  }
                if (password == "" || password == null || password == undefined) {
                    layer.msg('请填写密码', { icon: 5 });
                    return;
                }
                if (phone == "" || phone == null || phone == undefined) {
                    layer.msg('请填写手机号', { icon: 5 });
                    return;
                }
                //if (email == "" || email == null || email == undefined) {
                //    layer.msg('请填写邮箱', { icon: 5 });
                //    return;
                //}
                //if (storeid == "" || storeid == null || storeid == undefined) {
                //    layer.msg('请选择管辖店铺', { icon: 5 });
                //    return;
                //}
                if (sel_roles == "" || sel_roles == null || sel_roles == undefined) {
                    layer.msg('请选择角色', { icon: 5 });
                    return;
                  }
                  if (sel_manageqy == "" || sel_manageqy == null || sel_manageqy == undefined) {
                      layer.msg('请选择管理区域', { icon: 5 });
                      return;
                  }
                  if (sel_manageyw == "" || sel_manageyw == null || sel_manageyw == undefined) {
                      layer.msg('请选择管理业务', { icon: 5 });
                      return;
                  }
                  if (sel_managesp == "" || sel_managesp == null || sel_managesp == undefined) {
                      layer.msg('请选择管理商品', { icon: 5 });
                      return;
                  }
                  if (txt_loginid != "") {
                    var add = {
                        "txt_loginid": txt_loginid,
                        "txt_username": txt_username,
                        "password": password,
                        "phone": phone,
                      
                        "email": email,
                        "imgurl": imgurl,
                        "managestore": managestore,
                        "storeid": storeid,
                        "sel_roles": sel_roles,
                        "customername": customername,
                        "sel_manageqy": sel_manageqy,
                        "sel_manageyw": sel_manageyw,
                        "sel_managesp": sel_managesp,

                        "sex": sex,
                        "isactive": isactive,
                        "islookme": islookme
                    };
                    arr.push(add);
                    $.ajax({
                        url: 'ashx/list.ashx?type=add',
                        type: "post",
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify({ 'LoadData_type': arr }),
                        success: function (res) {
                            if (res.flag == "True") {
                                layer.close(index);
                                layer.msg(res.remsg, {
                                    time: 2000,
                                    end: function () {
                                        //执行重载
                                        table.reload('testReload', {
                                            where: {
                                                txt_loginid: $('#txt_loginid').val(),
                                                txt_username: $('#txt_username').val(),
                                                sel_roles: $('#sel_roles').val(),
                                                sel_isactive: $('#sel_isactive').val()
                                            }, page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                    }
                                })
                            }
                            else {
                                layer.alert(res.remsg);
                            }
                        }
                    })
                }
            });             
            submit.trigger('click');
          }
          }); 
          layer.full(index);
      }
    };
    
    $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
  </script>
</body>
</html>
